﻿using System;
using System.Windows;
using DSPL.Services;
using DSPL.Contracts;
using DSPL.Common;
using System.Data;
using DSPL.Services.Services;
using DSPL.Contracts.Common;
using DSPL.Contracts.ViewModel;
using DSPL.Contracts.Command;
using DSPL.Finac.Common;
using System.Windows.Input;

namespace DSPL.Finac.ViewModel
{
    public class CurrencyMasterViewModel : BaseViewModel
    {
        private bool isCurrencyCodeFocused;
        private bool isCurrencyNameFocused;
        private bool isCurrencyFromRateFocused;
        private bool isCurrencyToRateFocused;

        #region Define properties

        public bool IsCurrencyCodeFocused
        {
            get { return isCurrencyCodeFocused; }
            set
            {
                isCurrencyCodeFocused = value;
                this.OnPropertyChanged("IsCurrencyCodeFocused");
            }
        }

        public bool IsCurrencyNameFocused
        {
            get { return isCurrencyNameFocused; }
            set
            {
                isCurrencyNameFocused = value;
                this.OnPropertyChanged("IsCurrencyNameFocused");

            }
        }

        public bool IsCurrencyFromRateFocused
        {
            get { return isCurrencyFromRateFocused; }
            set
            {
                isCurrencyFromRateFocused = value;
                this.OnPropertyChanged("IsCurrencyFromRateFocused");

            }
        }

        public bool IsCurrencyToRateFocused
        {
            get { return isCurrencyToRateFocused; }
            set
            {
                isCurrencyToRateFocused = value;
                this.OnPropertyChanged("IsCurrencyToRateFocused");

            }
        }
        
        #endregion


        #region Constructor

        public CurrencyMasterViewModel()
            : base()
        {
            MessageTitle = "Currency Master";
        }

        #endregion
        /// <summary>
        /// Validate mandatory data before save
        /// </summary>
        /// <param name="obj"></param>
        protected override void PreDataValidateExecuted(object obj)
        {
            IsCurrencyCodeFocused = false;
            IsCurrencyNameFocused = false;
            IsCurrencyFromRateFocused = false;
            IsCurrencyToRateFocused = false;

            var messageService = new MessageService();
            if (string.IsNullOrEmpty(CurrentRow["CurCode"].ToString().Trim()))
            {
                messageService.ShowMessage(MessageText.CurrencyCodeNotEmpty, MessageTitle, MessageType.InfoOK);
                IsCurrencyCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (string.IsNullOrEmpty(CurrentRow["CurDesc"].ToString().Trim()))
            {
                messageService.ShowMessage(MessageText.CurrencyDescriptionNotEmpty, MessageTitle, MessageType.InfoOK);
                IsCurrencyNameFocused = true;
                this.IsValid = false;
                return;
            }

             if (CurrentRow["FromRate"]== DBNull.Value || Convert.ToDouble(CurrentRow["FromRate"]) ==0)
             {
                 messageService.ShowMessage(MessageText.FromRateNotZero, MessageTitle, MessageType.InfoOK);
                 IsCurrencyFromRateFocused = true;
                 this.IsValid = false;
                 return;
             }

             if (CurrentRow["ToRate"] == DBNull.Value || Convert.ToDouble(CurrentRow["ToRate"]) == 0)
             {
                 messageService.ShowMessage(MessageText.ToRateNotZero, MessageTitle, MessageType.InfoOK);
                 IsCurrencyToRateFocused = true;
                 this.IsValid = false;
                 return;
             }

             if (Convert.ToDouble(CurrentRow["FromRate"]) > Convert.ToDouble(CurrentRow["ToRate"]))
            {
                messageService.ShowMessage(MessageText.InvalidCurrencyRateRange, MessageTitle, MessageType.InfoOK);
                IsCurrencyFromRateFocused = true;
                this.IsValid = false;
                return;
            }
            this.IsValid = true;
        }

        protected override void RefreshExternalDataObjectsExecueted(object obj)
        {

            base.RefreshExternalDataObjectsExecueted(obj);

            if (FormMode == Modes.New)
            {
                CurrentRow["Active"] = 1;
                OnPropertyChanged("CurrentRow");

            }

        }

    }
}